package cn.sinata.wanglin;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import java.util.ArrayList;
import cn.sinata.wanglin.adapter.SendToMeAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.SendToMeBean;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
public class SendToMeActivity extends RecyclerListActivity {

    private BaseRecyclerAdapter adapter;
    private ArrayList<SendToMeBean> mDatas=new ArrayList<SendToMeBean>();
    private int pageNo = 0;
    @Override
    protected RecyclerView.Adapter setAdapter() {
        if(adapter==null){
            adapter=new SendToMeAdapter(mDatas,SendToMeActivity.this);
        }
        return adapter;

    }

    private void getData() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getSendMy(userid,pageNo).subscribe(new ResultDataSubscriber<ArrayList<SendToMeBean>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<SendToMeBean> data) {
                if (pageNo == 0) {
                    mDatas.clear();
                }
                if (data.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                mDatas.addAll(data);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("@我的");
        addRightButton("清空", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog("清空中...");
                clearAllMy();
            }


        });
        showDialog();
        getData();
    }
    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }
    private void clearAllMy() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.emptyMy(userid).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                pageNo = 0;
                mDatas.clear();
                adapter.notifyDataSetChanged();
            }
        });
    }

}
